#ifndef CLIENT_REQUEST_ANALYZER
#define CLIENT_REQUEST_ANALYZER

#include<string>
#include<map>
#include<vector>
#include "ClientSocketHandler.cpp"
#include"Defines.cpp"
#include<algorithm>

class ClientRequestAnalyzer
{
    public:
    struct RequestKeyData
    {
        public:
        std::string Key, Data;
    };
    private:
    bool _analyzeSuccessed;
    std::string _requestName, _url, _httpVersion;
    std::map<std::string, RequestKeyData> _keyDataMap;
    
    public:
    ClientRequestAnalyzer(ClientSocketHandler *clientSocketHandler)
    {
        std::vector<std::string> lines;
        std::string tempStr = "";
        while(1)
        {
            char c;
            if(clientSocketHandler->GetNextByte(&c) == false)
            {
                _analyzeSuccessed = false;
                return;
            }
            printf("%c", c);
            tempStr+=c;
            if(tempStr.length() >=2 && tempStr[tempStr.length()-1]=='\n' && tempStr[tempStr.length()-2]=='\r')
            {
                if(tempStr.length() == 2)
                {
                    break;
                }
                tempStr.erase(tempStr.length() - 2);
                lines.push_back(tempStr);
                tempStr.clear();
            }
        }
        AnalyzeFirstLine(lines[0]);
        for (int i = 1; i < lines.size(); i++)
        {
            AnalyzeDataLine(lines[i]);
        }
        _analyzeSuccessed = true;
    }

    bool AnalyzeSuccessed()
    {
        return _analyzeSuccessed;
    }
    void AnalyzeFirstLine(std::string line)
    {
        //GET /favicon.ico HTTP/1.1
        std::vector<std::string> strs;
        std::string tempStr = "";
        for(int i = 0;i<line.length();i++)
        {
            if(line[i] == ' ' && tempStr.length() > 0)
            {
                strs.push_back(tempStr);
                tempStr.clear();
                continue;
            }
            tempStr += line[i];
        }
        if(tempStr.length() > 0)
        {
            strs.push_back(tempStr);
        }
        _requestName = strs[0];
        _url = strs[1];
        _httpVersion = strs[2];
    }

    void AnalyzeDataLine(std::string line)
    {
        //Host: 127.0.0.1
        int i = 0;
        RequestKeyData keyData;
        for(i = 0;i<line.length();i++)
        {
            if(line[i] == ':')
            {
                keyData.Key = line.substr(0, i);
                i++;
                while(line[i] == ' ')
                {
                    i++;
                }
                break;
            }
        }
        keyData.Data = line.substr(i);
        std::transform(keyData.Key.begin(), keyData.Key.end(), keyData.Key.begin(), ::tolower);
        _keyDataMap.insert(std::pair<std::string, RequestKeyData>(keyData.Key, keyData));
    }

    std::string GetRequestName()
    {
        return _requestName;
    }
    std::string GetUrl()
    {
        return _url;
    }
    std::string GetHttpVersion()
    {
        return _httpVersion;
    }
    std::string GetKeyDataByKey(std::string key)
    {
        std::transform(key.begin(), key.end(), key.begin(), ::tolower);
        if(_keyDataMap.find(key) == _keyDataMap.end())
        {
            return "";
        }
        return _keyDataMap[key].Data;
    }
};

#endif